<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>登录</title>
    <link rel="stylesheet" href="./bootstrap/bootstrap.min.css" />
    <link rel="stylesheet" href="./bootstrap/bootstrap-icons.css" />
    <link rel="stylesheet" href="./css/common.css" />
    <link rel="stylesheet" href="./css/login.css" />
  </head>
  <body class="application application-offset">
    <div class="container-fluid container-application">
      <div class="main-content position-relative">
        <!-- Page content -->
        <div class="page-content">
          <div class="min-vh-100 py-5 d-flex align-items-center">
            <div class="w-100">
              <div class="row justify-content-center">
                <div class="col-sm-8 col-lg-4">
                  <div class="card shadow zindex-100 mb-0">
                    <div class="card-body px-md-5 py-5">
                      <div class="mb-5">
                        <h6 class="h3">传智教育-注册</h6>
                      </div>
                      <span class="clearfix"></span>
                      <form role="form" autocomplete="off">
                        <div class="form-group">
                          <label class="form-control-label">账号</label>
                          <div class="input-group input-group-merge">
                            <div class="input-group-prepend">
                              <span class="input-group-text"
                                ><i class="bi bi-person"></i
                              ></span>
                            </div>
                            <input
                              name="username"
                              type="text"
                              class="form-control"
                              id="input-email"
                              placeholder="请输入账号"
                            />
                          </div>
                        </div>
                        <div class="form-group mb-4">
                          <div
                            class="d-flex align-items-center justify-content-between"
                          >
                            <div>
                              <label class="form-control-label">密码</label>
                            </div>
                            <div class="mb-2">
                              <a
                                href="javascript:;"
                                class="small text-muted text-underline--dashed border-primary"
                                >忘记密码</a
                              >
                            </div>
                          </div>
                          <div class="input-group input-group-merge">
                            <div class="input-group-prepend">
                              <span class="input-group-text"
                                ><i class="bi bi-key"></i
                              ></span>
                            </div>
                            <input
                              name="password"
                              type="password"
                              class="form-control"
                              id="input-password"
                              placeholder="请输入密码"
                            />
                            <div class="input-group-append">
                              <span class="input-group-text">
                                <a
                                  href="javascript:;"
                                  data-toggle="password-text"
                                  data-target="#input-password"
                                >
                                  <i class="bi bi-eye-fill text-blue"></i>
                                </a>
                              </span>
                            </div>
                          </div>
                        </div>
                        <div class="mt-4">
                          <button
                            id="btn-register"
                            type="button"
                            class="btn btn-sm btn-info btn-icon btn-blue rounded-pill"
                          >
                            <span class="btn-inner--text">注册</span>
                            <span class="btn-inner--icon"
                              ><i class="bi bi-arrow-right"></i
                            ></span>
                          </button>
                        </div>
                      </form>
                    </div>
                    <div class="card-footer px-md-5">
                      <small>已经注册？</small>
                      <a
                        href="./login.html"
                        class="small font-weight-bold text-blue"
                        >去登录吧</a
                      >
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- toast -->
    <div
      class="position-fixed top-0 start-50 pt-4"
      style="z-index: 999; transform: translateX(-50%)"
    >
      <div id="myToast" class="toast bg-rgba">
        <div class="toast-body">提示消息</div>
      </div>
    </div>
    <script src="./bootstrap/bootstrap.min.js"></script>
    <script src="./lib/form-serialize.js"></script>
    <script src="./lib/axios.js"></script>
    <script src="./js/common copy.js"></script>
    <script>
      // 创建了toast轻提示 弹框告诉是否成功还是失败
   
      // tip是因为解构写到了js文档里面
      // tip('疯狂星期四 🐔 🥩')
      // ---------- 用户注册 ----------
      //1 获取按钮设置点击事件然后进行一系列操作
      document.querySelector('#btn-register').onclick =  async function () {
        // 利用插件获得所有的值
       const data =  serialize(document.querySelector('form'), {hash:true})
       console.log(data);
       //经典非空判断
       if(data.username===undefined||data.password===undefined){
        tip('疯狂星期四 🐔 🥩')
        return
       }
      //  把可能会犯错的值放到try里，
       try{
        const res = await axios({
          url:'/register',
          method:'post',
          data:data
        })
        // 打印找成功的位置
        console.log(res);
        // 弹出成功信息告诉用户
        tip(res.data.message)
        // 设置一个定时器延时登录
        setTimeout(function () {
          location.assign('./login copy.html')
        },1000)
       }catch(err){
        console.dir(err);
        // 如果错误就是已经注册，就要弹出信息然后提示
        tip(err.response.message)
       }
        
      }

      // console.log(1);
      // 获取表单的所有值，利用那个插件
      // 判断值是不是为空，如果为空就提示用户少输入
      // 然后进行返回
      // 再次判断，如果密码低于6位就弹出信息提示
      // 严禁操作，把有可能报错的代码放在try里面，然后catch捕捉，
      // 获取借口
      // 弹出注册成功信息
      // 清空数据
      // 设置一个计时器表示什么时候加载到别的页面
      // 加载到什么页面
      // 如果错啦就捕获错误并且弹出信息提示
    </script>
  </body>
</html>
